package com.bw.goods.mvvm.viewmodel;

import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.Utils;
import com.bw.goods.mvvm.adapter.CategoryGoodsAdapter;
import com.bw.goods.mvvm.adapter.CategoryTextAdapter;
import com.bw.goods.mvvm.model.ChanageModel;
import com.bw.goods.mvvm.model.entity.CategoryValueEntity;
import com.bw.goods.mvvm.model.entity.ChanagEntity;
import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author KK
 * @description:
 * @date :2021/10/15 18:34
 */
public class CategoryViewModel extends BaseViewModel<ChanageModel> implements OnItemClickListener{
    CategoryValueEntity categoryValueEntity;
    public List<ChanagEntity> list = new ArrayList<ChanagEntity>();
   public BaseQuickAdapter changeGoodsAdapter = new CategoryGoodsAdapter();
   public BaseQuickAdapter changeTextAdapter = new CategoryTextAdapter();
    @Override
    public void oncreate() {
        super.oncreate();
        requestChanage();

    }

    public void requestChanage(){

        list.add(new ChanagEntity(0,"电脑办公"));
        list.add(new ChanagEntity(1,"手机数码"));
        list.add(new ChanagEntity(2,"男装"));
        list.add(new ChanagEntity(3,"女装"));
        list.add(new ChanagEntity(4,"家用电器"));


        changeTextAdapter.setNewInstance(list);
        changeTextAdapter.setOnItemClickListener(this);

        HashMap<String, Object>  map = new HashMap<>();
        map.put("parentId","1");
        m.requestChanage(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);

    }

    @Override
    protected void result(BaseEntity baseEntity) {
        if(baseEntity instanceof CategoryValueEntity){
            categoryValueEntity = (CategoryValueEntity) baseEntity;
            changeGoodsAdapter.setNewInstance(categoryValueEntity.data);
        }
        
        if(baseEntity.getMessage().equals("分类列表为空")){
            ArrayList<CategoryValueEntity.DataBean> list = new ArrayList<>();
            CategoryValueEntity.DataBean dataBean = new CategoryValueEntity.DataBean();
            dataBean.setCategory_name("本页面没有数据哟");
            list.add(dataBean);
            changeGoodsAdapter.setNewInstance(list);

        }
    }



    @Override
    public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
        LogUtils.e("dad");
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("parentId",position+1);
        m.requestChanage(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);




    }
}
